<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Procedural parameter</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Procedural_parameter"> <link href="./mw/ext.math.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Procedural_parameter rootpage-Procedural_parameter skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Procedural parameter</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><p>In <a href="Computing" title="Computing">computing</a>, a <b>procedural parameter</b> is a <a href="Parameter_(computer_science)" class="mw-redirect" title="Parameter (computer science)">parameter</a> of a <a href="Subroutine" class="mw-redirect" title="Subroutine">procedure</a> that is itself a procedure.
</p><p>This concept is an extremely powerful and versatile <a href="Computer_programming" title="Computer programming">programming</a> tool, because it allows programmers to modify certain steps of a <a href="Library_(computer_science)" class="mw-redirect" title="Library (computer science)">library procedure</a> in arbitrarily complicated ways, without having to understand or modify the code of that procedure.
</p><p>This tool is particularly effective and convenient in languages that support <a href="Nested_function" title="Nested function">local function definitions</a>, such as <a href="Pascal_programming_language" class="mw-redirect" title="Pascal programming language">Pascal</a> and the modern <a href="GNU_Compiler_Collection" title="GNU Compiler Collection">GNU dialect</a> of <a href="C_(programming_language)" title="C (programming language)">C</a>. It is even more so when <a href="Closure_(computer_science)" class="mw-redirect" title="Closure (computer science)">function closures</a> are available. The same functionality (and more) is provided by <a href="Object_(computing)" class="mw-redirect" title="Object (computing)">objects</a> in <a href="Object_oriented_programming" class="mw-redirect" title="Object oriented programming">object oriented</a> <a href="Programming_language" title="Programming language">programming languages</a>, but at a significantly higher cost.
</p><p>Procedural parameters are somewhat related to the concepts of <a href="First-class_function" title="First-class function">first-class function</a> and <a href="Anonymous_function" title="Anonymous function">anonymous function</a>, but is distinct from them. These two concepts have more to do with how functions are defined, rather than how they are used.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Basic_concept">Basic concept</h2></div>
<p>In most languages that provide this feature, a procedural parameter <i>f</i> of a subroutine <i>P</i> can be called inside the body of <i>P</i> as if it were an ordinary procedure:
</p>
<pre><b>procedure</b> <i>P</i>(<i>f</i>):
<b>return</b> <i>f</i>(6,3) * <i>f</i>(2,1)
</pre>
<p>When calling the subroutine <i>P</i>, one must give it one argument, that must be some previously defined function compatible with the way <i>P</i> uses its parameter <i>f</i>. For example, if we define
</p>
<pre><b>procedure</b> <i>plus</i>(<i>x</i>, <i>y</i>):
<b>return</b> <i>x</i> + <i>y</i>
</pre>
<p>then we may call <i>P</i> (<i>plus</i>), and the result will be <i>plus</i>(6,3) * <i>plus</i>(2,1) = (6 + 3)*(2 + 1) = 27. On the other hand, if we define
</p>
<pre><b>procedure</b> <i>quot</i>(<i>u</i>, <i>v</i>):
<b>return</b> <i>u</i>/<i>v</i>
</pre>
<p>then the call <i>P</i> (<i>quot</i>) will return <i>quot</i>(6,3)*<i>quot</i>(2,1) = (6/3)*(2/1) = 4. Finally, if we define
</p>
<pre><b>procedure</b> <i>evil</i>(<i>z</i>)
<b>return</b> z + 100
</pre>
<p>then the call <i>P</i> (<i>evil</i>) will not make much sense, and may be flagged as an error.
</p>
<div class="mw-heading mw-heading3"><h3 id="Syntactic_details">Syntactic details</h3></div>
<p>Some programming languages that have this feature may allow or require a complete type declaration for each procedural parameter <i>f</i>, including the number and type of its arguments, and the type of its result, if any. For example, in the C programming language the example above could be written as
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="nf">P</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">f</span><span class="p">)(</span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">f</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">f</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>In principle, the actual function <i>actf</i> that is passed as argument when <i>P</i> is called must be type-compatible with the declared type of the procedure parameter <i>f</i>. This usually means that <i>actf</i> and <i>f</i> must return the same type of result, must have the same number of arguments, and corresponding arguments must have the same type. The names of the arguments need not be the same, however, as shown by the <i>plus</i> and <i>quot</i> examples above. However, some programming languages may be more restrictive or more liberal in this regard.
</p>
<div class="mw-heading mw-heading3"><h3 id="Scoping">Scoping</h3></div>
<p>In languages that allow procedural parameters, the scoping rules are usually defined in such a way that procedural parameters are executed in their native scope. More precisely, suppose that the function <i>actf</i> is passed as argument to <i>P</i>, as its procedural parameter <i>f</i>; and <i>f</i> is then called from inside the body of <i>P</i>. While <i>actf</i> is being executed, it sees the environment of its definition.
</p><p>The implementation of these scoping rules is not trivial. By the time that <i>actf</i> is finally executed, the <a href="Call_stack#Structure" title="Call stack">activation records</a> where its environment variables live may be arbitrarily deep in the stack. This is the so-called <a href="Funarg_problem#Downwards_funarg_problem" title="Funarg problem">downwards funarg problem</a>.
</p>
<div class="mw-heading mw-heading2"><h2 id="Example:_Generic_insertion_sort">Example: Generic insertion sort</h2></div>
<p>The concept of procedural parameter is best explained by examples. A typical application is the following generic implementation of the <a href="Insertion_sort" title="Insertion sort">insertion sort</a> algorithm, that takes two integer parameters <i>a</i>,<i>b</i> and two procedural parameters <i>prec</i>, <i>swap</i>:
</p>
<pre><b>procedure</b> <i>isort</i>(<i>a</i>, <i>b</i>, <i>prec</i>, <i>swap</i>):
<b>integer</b> <i>i</i>, <i>j</i>;
<i>i</i> ← <i>a</i>;
<b>while</b> <i>i</i> ≤ <i>b</i> <b>do</b>
<i>j</i> ← <i>i</i>;
<b>while</b> <i>j</i> > <i>a</i> <b>and</b> <i>prec</i>(<i>j</i>, <i>j</i>−1) <b>do</b>
<i>swap</i>(<i>j</i>, <i>j</i>−1);
<i>j</i> ← <i>j</i>−1;
<i>i</i> ← <i>i</i>+1;
</pre>
<p>This procedure can be used to sort the elements <i>x</i>[<i>a</i>] through <i>x</i>[<i>b</i>] of some array <i>x</i>, of arbitrary <a href="Datatype" class="mw-redirect" title="Datatype">type</a>, in a user-specified order. The parameters <i>prec</i> and <i>swap</i> should be two <a href="Function_(computing)" class="mw-redirect" title="Function (computing)">functions</a>, defined by the client, both taking two integers <i>r</i>, <i>s</i> between <i>a</i> and <i>b</i>. The <i>prec</i> function should return <b>true</b> <a href="If_and_only_if" title="If and only if">if and only if</a> the data stored in <i>x</i>[<i>r</i>] should precede the data stored in <i>x</i>[<i>s</i>], in the ordering defined by the client. The <i>swap</i> function should exchange the contents of <i>x</i>[<i>r</i>] and <i>x</i>[<i>s</i>], and return no result.
</p><p>By the proper choice of the functions <i>prec</i> and <i>swap</i>, the same <i>isort</i> procedure can be used to reorder arrays of any data type, stored in any medium and organized in any <a href="Data_structure" title="Data structure">data structure</a> that provides indexed access to individual array elements. (Note however that there are <a href="Sorting_algorithm" title="Sorting algorithm">sorting algorithms</a> that are much more efficient than insertion sort for large arrays.)
</p>
<div class="mw-heading mw-heading3"><h3 id="Sorting_floating-point_numbers">Sorting floating-point numbers</h3></div>
<p>For instance, we can sort an array <i>z</i> of 20 floating-point numbers, <i>z</i>[1] through <i>z</i>[20] in increasing order by calling <i>isort</i> (1, 20,<i>zprec</i>,<i>zswap</i>), where the functions <i>zprec</i> and <i>zswap</i> are defined as
</p>
<pre><b>procedure</b> <i>zprec</i>(<i>r</i>, <i>s</i>):
<b>return</b> (<i>z</i>[<i>r</i>] < <i>z</i>[<i>s</i>]);
<b>procedure</b> <i>zswap</i>(<i>r</i>, <i>s</i>):
<b>float</b> <i>t</i>;
<i>t</i> ← <i>z</i>[<i>r</i>];
<i>z</i>[<i>r</i>] ← <i>z</i>[<i>s</i>];
<i>z</i>[<i>s</i>] ← <i>t</i>
</pre>
<div class="mw-heading mw-heading3"><h3 id="Sorting_rows_of_a_matrix">Sorting rows of a matrix</h3></div>
<p>For another example, let <i>M</i> be a matrix of integers with 10 rows and 20 columns, with indices starting at 1. The following code will rearrange the elements in each row so that all the even values come before all odd values:
</p>
<pre><b>integer</b> i
<b>procedure</b> <i>eoprec</i>(<i>r</i>, <i>s</i>):
<b>return</b> (<i>M</i>[<i>i</i>, <i>r</i>] <b>mod</b> 2) < (<i>M</i>[<i>i</i>, <i>s</i>] <b>mod</b> 2);
<b>procedure</b> <i>eoswap</i>(<i>r</i>, <i>s</i>):
<b>integer</b> <i>t</i>;
<i>t</i> ← <i>M</i>[<i>i</i>,<i>r</i>];
<i>M</i>[<i>i</i>,<i>r</i>] ← <i>M</i>[<i>i</i>,<i>s</i>];
<i>M</i>[<i>i</i>,<i>s</i>] ← <i>t</i>;
<b>for</b> <i>i</i> from 1 <b>to</b> 10 <b>do</b>
<i>isort</i>(1, 20, eoprec, eoswap);
</pre>
<p>Note that the effects of <i>eoprec</i> and <i>eoswap</i> depend on the row number <i>i</i>, but the <i>isort</i> procedure does not need to know that.
</p>
<div class="mw-heading mw-heading3"><h3 id="Vector-sorting_procedure">Vector-sorting procedure</h3></div>
<p>The following example uses <i>isort</i> to define a procedure <i>vecsort</i> that takes an integer <i>n</i> and an integer vector <i>v</i> with elements <i>v</i>[0] through <i>v</i>[<i>n</i>−1] and sorts them in either increasing or decreasing order, depending on whether a third parameter <i>incr</i> is <b>true</b> or <b>false</b>, respectively:
</p>
<pre><b>procedure</b> <i>vecsort</i>(<i>n</i>, <i>v</i>, <i>incr</i>):
<b>procedure</b> <i>vprec</i>(<i>r</i>, <i>s</i>):
<b>if</b> <i>incr</i> <b>then</b>
<b>return</b> <i>v</i>[<i>r</i>] < <i>v</i>[<i>s</i>];
<b>else</b>
<b>return</b> <i>v</i>[<i>r</i>] > <i>v</i>[<i>s</i>];
<b>procedure</b> <i>vswap</i>(<i>r</i>, <i>s</i>):
<b>integer</b> <i>t</i>;
<i>t</i> ← <i>v</i>[<i>r</i>];
<i>v</i>[<i>r</i>] ← <i>v</i>[<i>s</i>];
<i>v</i>[<i>s</i>] ← <i>t</i>
<i>isort</i>(0, <i>n</i>−1, <i>vprec</i>, <i>vswap</i>);
</pre>
<p>Note the use of nested function definitions to get a function <i>vprec</i> whose effect depends on the parameter <i>incr</i> passed to <i>vecsort</i>. In languages that do not allow nested function definitions, like standard C, obtaining this effect would require rather complicated and/or <a href="Thread_safety" title="Thread safety">thread-unsafe</a> code.
</p><p><br>
</p>
<div class="mw-heading mw-heading2"><h2 id="Example:_merging_two_sequences">Example: merging two sequences</h2></div>
<p>The following example illustrates the use of procedural parameters to process abstract data structures independently of their concrete implementation. The problem is to merge two ordered sequences of records into a single sorted sequence, where the nature of the records and the ordering criterion can be chosen by the client. The following implementation assumes only that each record can be referenced by a <a href="Memory_address" title="Memory address">memory address</a>, and there is a "null address" Λ that is not the address of any valid record. The client must provide the addresses <i>A</i>, <i>B</i> of the first records in each sequence, and functions <i>prec</i>, <i>next</i>, and <i>append</i>, to be described later.
</p>
<pre><b>procedure</b> <i>merge</i>(<i>A</i>, <i>B</i>, <i>prec</i>, <i>nextA</i>, <i>appendA</i>, <i>nextB</i>, <i>appendB</i>):
<b>address</b> <i>ini</i>, <i>fin</i>, <i>t</i>
<i>ini</i> ← Λ; <i>fin</i> ← Λ
<b>while</b> <i>A</i> ≠ Λ or <i>B</i> ≠ Λ <b>do</b>
<b>if</b> <i>B</i> = Λ <b>or</b> (<i>A</i> ≠ Λ <b>and</b> <i>B</i> ≠ Λ <b>and</b> <i>prec</i>(<i>A</i>, <i>B</i>)) <b>then</b>
<i>t</i> ← <i>nextA</i>(<i>A</i>)
<i>fin</i> ← appendA(<i>A</i>, <i>fin</i>); <b>if</b> <i>ini</i> = Λ <b>then</b> <i>ini</i> ← <i>fin</i>
<i>A</i> ← <i>t</i>
<b>else</b>
<i>t</i> ← <i>nextB</i>(<i>B</i>)
<i>fin</i> ← <i>appendB</i>(<i>B</i>, <i>fin</i>); <b>if</b> <i>ini</i> = Λ <b>then</b> <i>ini</i> ← <i>fin</i>
<i>B</i> ← <i>t</i>
<b>return</b> <i>ini</i>
</pre>
<p>The function <i>prec</i> should take the addresses <i>r</i>, <i>s</i> of two records, one from each sequence, and return <b>true</b> if the first record should come before the other in the output sequence. The function <i>nextA</i> should take the address of a record from the first sequence, and return the address of the next record in the same sequence, or Λ if there is none. The function <i>appendA</i> should append the first record from sequence <i>A</i> to the output sequence; its arguments are the address <i>A</i> of the record to be appended, and the address <i>fin</i> of the last record of the output list (or Λ if that list is still empty). The procedure <i>appendA</i> should return the updated address of the final element of the output list. The procedures <i>nextB</i> and <i>appendB</i> are analogous for the other input sequence.
</p>
<div class="mw-heading mw-heading3"><h3 id="Merging_linked_lists">Merging linked lists</h3></div>
<p>To illustrate the use of the generic merge procedure, here is the code for merging two simple <a href="Linked_list" title="Linked list">linked lists</a>, starting with nodes at addresses <i>R</i>, <i>S</i>. Here we assume that each record <i>x</i> contains an integer field <i>x</i>.<i>INFO</i> and an address field <i>x</i>.<i>NEXT</i> that points to the next node; where the <i>info</i> fields are in increasing order in each list. The input lists are dismantled by the merge, and their nodes are used to build the output list.
</p>
<pre><b>procedure</b> <i>listmerge</i>(<i>R</i>, <i>S</i>):
<b>procedure</b> <i>prec</i>(<i>r</i>, <i>s</i>):
<b>return</b> <i>r</i>.<i>INFO</i> < <i>s</i>.<i>INFO</i>
<b>procedure</b> <i>next</i>(<i>x</i>):
<b>return</b> <i>x</i>.<i>NEXT</i>
<b>procedure</b> <i>append</i>(<i>x</i>, <i>fin</i>)
<b>if</b> <i>fin</i> ≠ Λ <b>then</b> <i>fin</i>.<i>NEXT</i> ← <i>x</i>
<i>x</i>.<i>NEXT</i> ← Λ
<b>return</b> <i>x</i>
<b>return</b> <i>merge</i>(<i>R</i>, <i>S</i>, <i>prec</i>, <i>next</i>, <i>append</i>, <i>next</i>, <i>append</i>)
</pre>
<div class="mw-heading mw-heading3"><h3 id="Merging_vectors">Merging vectors</h3></div>
<p>The following code illustrates the independence of the generic <i>merge</i> procedure from the actual representation of the sequences. It merges the elements of two ordinary arrays <i>U</i>[0] through <i>U</i>[<i>m</i>−1] and <i>V</i>[0] through <i>V</i>[<i>n</i>−1] of floating-point numbers, in decreasing order. The input arrays are not modified, and the merged sequence of values is stored into a third vector <i>W</i>[0] through <i>W</i>[<i>m</i>+<i>n</i>−1]. As in the C programming language, we assume that the expression "&<i>V</i>" yields the address of variable <i>V</i>, "*<i>p</i>" yields the variable whose address is the value of <i>p</i>, and that "&(<i>X</i>[<i>i</i>])" is equivalent to "&(<i>X</i>[0]) + <i>i</i>" for any array <i>X</i> and any integer <i>i</i>.
</p>
<pre><b>procedure</b> <i>arraymerge</i>(<i>U</i>, <i>m</i>, <i>V</i>, <i>n</i>, <i>W</i>):
<b>procedure</b> <i>prec</i>(<i>r</i>, <i>s</i>):
<b>return</b> (*<i>r</i>) > (*<i>s</i>)
<b>procedure</b> <i>nextU</i>(<i>x</i>):
<b>if</b> <i>x</i> = &(<i>U</i>[<i>m</i>−1]) <b>then</b> <b>return</b> Λ <b>else</b> <b>return</b> <i>x</i> + 1
<b>procedure</b> <i>nextV</i>(<i>x</i>):
<b>if</b> <i>x</i> = &(<i>V</i>[<i>n</i>−1]) <b>then</b> <b>return</b> Λ <b>else</b> <b>return</b> <i>x</i> + 1
<b>procedure</b> <i>append</i>(<i>x</i>, <i>fin</i>)
<b>if</b> <i>fin</i> = Λ <b>then</b> <i>fin</i> ← &(<i>W</i>[0])
(*<i>fin</i>) ← (*<i>x</i>)
<b>return</b> <i>fin</i> + 1
<b>if</b> <i>m</i> = 0 then <i>U</i> ← Λ
<b>if</b> <i>n</i> = 0 then <i>V</i> ← Λ
<b>return</b> <i>merge</i>(<i>U</i>, <i>V</i>, <i>prec</i>, <i>nextU</i>, <i>append</i>, <i>nextV</i>, <i>append</i>)
</pre>
<div class="mw-heading mw-heading2"><h2 id="Example:_Definite_integral">Example: Definite integral</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Integrating_over_an_interval">Integrating over an interval</h3></div>
<p>The following procedure computes the approximate <a href="Integral_(mathematics)" class="mw-redirect" title="Integral (mathematics)">integral</a> <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \textstyle \int _{a}^{b}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mstyle displaystyle="false" scriptlevel="0">
<msubsup>
<mo>∫<!-- ∫ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>a</mi>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mi>b</mi>
</mrow>
</msubsup>
</mstyle>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \textstyle \int _{a}^{b}}</annotation>
</semantics>
</math></span><img src="./b53a1a66659263a9b99efd1f4347d19417b9ce40.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -1.005ex; width:2.452ex; height:3.676ex;" alt="{\displaystyle \textstyle \int _{a}^{b}}" loading="lazy"></span> <i>f</i> (<i>x</i>) d<i>x</i> of a given real-valued <a href="Function_(mathematics)" title="Function (mathematics)">function</a> <i>f</i> over a given interval [<i>a</i>,<i>b</i>] of the <a href="Real_number" title="Real number">real line</a>. The <a href="Numerical_method" title="Numerical method">numerical method</a> used is the <a href="Trapezium_rule" class="mw-redirect" title="Trapezium rule">trapezium rule</a> with a given number <i>n</i> of steps; the real numbers are approximated by floating-point numbers.
</p>
<pre><b>procedure</b> <i>Intg</i>(<i>f</i>, <i>a</i>, <i>b</i>, <i>n</i>):
<b>float</b> <i>t</i>, <i>x</i>, <i>s</i>; <b>integer</b> <i>i</i>
<b>if</b> <i>b</i> = <i>a</i> <b>then</b> <b>return</b> 0
<i>x</i> ← <i>a</i>; <i>s</i> ← <i>f</i>(<i>a</i>) / 2;
<b>for</b> i <b>from</b> 1 <b>to</b> <i>n</i>−1 <b>do</b>
<i>t</i> ← <i>i</i>/(<i>n</i>+1); <i>x</i> ← (1−<i>t</i>) * <i>a</i> + <i>t</i> * <i>b</i>;
<i>s</i> ← <i>s</i> + <i>f</i>(<i>x</i>)
<i>s</i> ← <i>f</i>(<i>b</i>) / 2
<b>return</b> (<i>b</i> − <i>a</i>) * <i>s</i> / <i>n</i>
</pre>
<div class="mw-heading mw-heading3"><h3 id="Integrating_over_a_disk">Integrating over a disk</h3></div>
<p>Consider now the problem of integrating a given function <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle g}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>g</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle g}</annotation>
</semantics>
</math></span><img src="./d3556280e66fe2c0d0140df20935a6f057381d77.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:1.116ex; height:2.009ex;" alt="{\displaystyle g}" loading="lazy"></span>, with two arguments, over a disk <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle D}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>D</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle D}</annotation>
</semantics>
</math></span><img src="./f34a0c600395e5d4345287e21fb26efd386990e6.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.924ex; height:2.176ex;" alt="{\displaystyle D}" loading="lazy"></span> with given center (<span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle xc,yc}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>x</mi>
<mi>c</mi>
<mo>,</mo>
<mi>y</mi>
<mi>c</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle xc,yc}</annotation>
</semantics>
</math></span><img src="./71b6c4db6efaefa693ff8357c47551165d5f6bcd.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:5.533ex; height:2.009ex;" alt="{\displaystyle xc,yc}" loading="lazy"></span>) and given radius <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle R}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>R</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle R}</annotation>
</semantics>
</math></span><img src="./4b0bfb3769bf24d80e15374dc37b0441e2616e33.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.764ex; height:2.176ex;" alt="{\displaystyle R}" loading="lazy"></span>. This problem can be reduced to two nested single-variable integrals by the change of variables
</p>
<dl><dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \int \!\int _{D}g(x,y)\,\mathrm {d} x\,\mathrm {d} y=\int _{0}^{R}z\left(\int _{0}^{2\pi }g({\mathit {xc}}+z\cos t,{\mathit {yc}}+z\sin t)\,\mathrm {d} t\right)\,\mathrm {d} z}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo>∫<!-- ∫ --></mo>
<mspace width="negativethinmathspace"></mspace>
<msub>
<mo>∫<!-- ∫ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>D</mi>
</mrow>
</msub>
<mi>g</mi>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo>,</mo>
<mi>y</mi>
<mo stretchy="false">)</mo>
<mspace width="thinmathspace"></mspace>
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="normal">d</mi>
</mrow>
<mi>x</mi>
<mspace width="thinmathspace"></mspace>
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="normal">d</mi>
</mrow>
<mi>y</mi>
<mo>=</mo>
<msubsup>
<mo>∫<!-- ∫ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>0</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mi>R</mi>
</mrow>
</msubsup>
<mi>z</mi>
<mrow>
<mo>(</mo>
<mrow>
<msubsup>
<mo>∫<!-- ∫ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mn>0</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
<mi>π<!-- π --></mi>
</mrow>
</msubsup>
<mi>g</mi>
<mo stretchy="false">(</mo>
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi class="MJX-tex-mathit" mathvariant="italic">x</mi>
<mi class="MJX-tex-mathit" mathvariant="italic">c</mi>
</mrow>
</mrow>
<mo>+</mo>
<mi>z</mi>
<mi>cos</mi>
<mo><!-- --></mo>
<mi>t</mi>
<mo>,</mo>
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi class="MJX-tex-mathit" mathvariant="italic">y</mi>
<mi class="MJX-tex-mathit" mathvariant="italic">c</mi>
</mrow>
</mrow>
<mo>+</mo>
<mi>z</mi>
<mi>sin</mi>
<mo><!-- --></mo>
<mi>t</mi>
<mo stretchy="false">)</mo>
<mspace width="thinmathspace"></mspace>
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="normal">d</mi>
</mrow>
<mi>t</mi>
</mrow>
<mo>)</mo>
</mrow>
<mspace width="thinmathspace"></mspace>
<mrow class="MJX-TeXAtom-ORD">
<mi mathvariant="normal">d</mi>
</mrow>
<mi>z</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \int \!\int _{D}g(x,y)\,\mathrm {d} x\,\mathrm {d} y=\int _{0}^{R}z\left(\int _{0}^{2\pi }g({\mathit {xc}}+z\cos t,{\mathit {yc}}+z\sin t)\,\mathrm {d} t\right)\,\mathrm {d} z}</annotation>
</semantics>
</math></span><img src="./36302270940ead35c1f046d53e9b45550e90788a.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -2.505ex; width:65.961ex; height:6.343ex;" alt="{\displaystyle \int \!\int _{D}g(x,y)\,\mathrm {d} x\,\mathrm {d} y=\int _{0}^{R}z\left(\int _{0}^{2\pi }g({\mathit {xc}}+z\cos t,{\mathit {yc}}+z\sin t)\,\mathrm {d} t\right)\,\mathrm {d} z}" loading="lazy"></span></dd></dl>
<p>The following code implements the <a href="Sides_of_an_equation" title="Sides of an equation">right-hand-side formula</a>:
</p>
<pre><b>procedure</b> <i>DiskIntg</i>(<i>g</i>, <i>xc</i>, <i>yc</i>, <i>R</i>, <i>n</i>)
<b>procedure</b> <i>gring</i>(<i>z</i>):
<b>procedure</b> <i>gpolar</i>(<i>t</i>):
<b>float</b> <i>x</i>, <i>y</i>
<i>x</i> ← <i>xc</i> + <i>z</i> * <i>cos</i>(<i>t</i>)
<i>y</i> ← <i>yc</i> + <i>z</i> * <i>sin</i>(<i>t</i>)
<b>return</b> <i>g</i>(<i>x</i>, <i>y</i>)
<b>integer</b> <i>m</i> ← <i>round</i>(<i>n</i>*<i>z</i>/<i>R</i>)
<b>return</b> <i>z</i> * <i>Intg</i>(<i>gpolar</i>, 0, 2*π, <i>m</i>)
<b>return</b> <i>Intg</i>(<i>gring</i>, 0, <i>R</i>, <i>n</i>)
</pre>
<p>This code uses the integration procedure <i>Intg</i> in two levels. The outer level (last line) uses <i>Intg</i> to compute the integral of <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle gring(z)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>g</mi>
<mi>r</mi>
<mi>i</mi>
<mi>n</mi>
<mi>g</mi>
<mo stretchy="false">(</mo>
<mi>z</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle gring(z)}</annotation>
</semantics>
</math></span><img src="./bf7ec7b065e42d57d08f722b7f437f71de2e8379.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:8.375ex; height:2.843ex;" alt="{\displaystyle gring(z)}" loading="lazy"></span> for <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle z}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>z</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle z}</annotation>
</semantics>
</math></span><img src="./bf368e72c009decd9b6686ee84a375632e11de98.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.088ex; height:1.676ex;" alt="{\displaystyle z}" loading="lazy"></span> varying from 0 to <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle R}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>R</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle R}</annotation>
</semantics>
</math></span><img src="./4b0bfb3769bf24d80e15374dc37b0441e2616e33.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.764ex; height:2.176ex;" alt="{\displaystyle R}" loading="lazy"></span>. The inner level (next-to-last line) defines <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle gring(z)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>g</mi>
<mi>r</mi>
<mi>i</mi>
<mi>n</mi>
<mi>g</mi>
<mo stretchy="false">(</mo>
<mi>z</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle gring(z)}</annotation>
</semantics>
</math></span><img src="./bf7ec7b065e42d57d08f722b7f437f71de2e8379.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:8.375ex; height:2.843ex;" alt="{\displaystyle gring(z)}" loading="lazy"></span> as being the <a href="Line_integral" title="Line integral">line integral</a> of <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle g(x,y)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>g</mi>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo>,</mo>
<mi>y</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle g(x,y)}</annotation>
</semantics>
</math></span><img src="./bf358a54b0375e22ae5f3ab2c3e1a22c0c87e11c.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:6.444ex; height:2.843ex;" alt="{\displaystyle g(x,y)}" loading="lazy"></span> over the circle with center <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle (xc,yc)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo stretchy="false">(</mo>
<mi>x</mi>
<mi>c</mi>
<mo>,</mo>
<mi>y</mi>
<mi>c</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle (xc,yc)}</annotation>
</semantics>
</math></span><img src="./4f9380cd74ca531ea9fc771057723309bc1386d9.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:7.342ex; height:2.843ex;" alt="{\displaystyle (xc,yc)}" loading="lazy"></span> and radius <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle z}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>z</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle z}</annotation>
</semantics>
</math></span><img src="./bf368e72c009decd9b6686ee84a375632e11de98.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.088ex; height:1.676ex;" alt="{\displaystyle z}" loading="lazy"></span>.
</p>
<div class="mw-heading mw-heading2"><h2 id="History">History</h2></div>
<p>Procedural parameters were invented before the age of electronic computers, by <a href="Mathematician" title="Mathematician">mathematician</a> <a href="Alonzo_Church" title="Alonzo Church">Alonzo Church</a>, as part of his <a href="Lambda_calculus" title="Lambda calculus">lambda calculus</a> model of computation.
</p><p>Procedural parameters as a programming language feature were introduced by <a href="ALGOL_60_programming_language" class="mw-redirect" title="ALGOL 60 programming language">ALGOL 60</a>. In fact, ALGOL 60 had a powerful "<a href="Call_by_name" class="mw-redirect" title="Call by name">call by name</a>" parameter-passing mechanism that could simplify some uses of procedural parameters; see <a href="Jensen's_Device" class="mw-redirect" title="Jensen's Device">Jensen's Device</a>.
</p><p>Procedural parameters were an essential feature of the <a href="LISP_programming_language" class="mw-redirect" title="LISP programming language">LISP programming language</a>, which also introduced the concept of function closure or <a href="Funarg" class="mw-redirect" title="Funarg">funarg</a>. The <a href="C_(programming_language)" title="C (programming language)">C programming language</a> allows <a href="Function_pointers" class="mw-redirect" title="Function pointers">function pointers</a> to be passed as parameters, which accomplish the same end, and are often used as <a href="Callbacks" class="mw-redirect" title="Callbacks">callbacks</a> in <a href="Event-driven_programming" title="Event-driven programming">event-driven programming</a> and as error handlers. However, only a few modern C compilers allow nested function definitions, so that its other uses are relatively uncommon. Procedural parameters were provided also in Pascal, together with nested procedure definitions; however, since standard Pascal did not allow separate compilation, the feature was little used in that language, too.
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Function_pointer" title="Function pointer">Function pointer</a></li>
<li><a href="Functional_programming" title="Functional programming">Functional programming</a></li>
<li><a href="Funarg_problem" title="Funarg problem">Funarg problem</a></li>
<li><a href="Design_patterns_(computer_science)" class="mw-redirect" title="Design patterns (computer science)">Design patterns (computer science)</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
</div>
<style data-mw-deduplicate="TemplateStyles:r1251242444">
/* start https://en.wikipedia.org/ */
.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-05-14" href="https://en.wikipedia.org/wiki/?title=Procedural_parameter&oldid=1290309154">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>